package com.lxr0860.lxr0860production_test.itemactivity;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.lxr0860.lxr0860production_test.R;
import com.lxr0860.lxr0860production_test.utils.Contants;
import com.lxr0860.lxr0860production_test.view.CustomDialog;


public class LCDActivity extends Activity implements View.OnClickListener {
    private static final int RED = Color.RED;
    private static final int GREEN = Color.GREEN;
    private static final int BLUE = Color.BLUE;
    private static final int BLACK = Color.BLACK;
    private static final int WHITE = Color.WHITE;
    private ImageView iv_lcd_bg;
    private TextView tv_lcd_tip;
    private SeekBar sb_lcd_brightness;
    private int mCurrState = RED;
    private static final int MAX_BRIGHTNESS = 255;
    Resources resources;
    private int testType;
    private boolean maxChanged,minChanged;
    private CustomDialog.Builder mBuilder;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case Contants.DIALOG_POSITIVE:
                    Intent mIntent=new Intent();
                    Bundle bundle=new Bundle();
                    bundle.putBoolean("result",true);
                    mIntent.putExtras(bundle);
                    LCDActivity.this.setResult(Contants.TEST_ITEM_LCD,mIntent);
                    finish();
                    break;
                case Contants.DIALOG_NEGATIVE:
                    finish();
                    break;
            }
        }
    };
    private TextView mTips;


    private void init() {
        testType = getIntent().getIntExtra("testType", Contants.TEST_ZUZHUANG);
        resources = this.getResources();
        iv_lcd_bg = (ImageView) findViewById(R.id.iv_lcd_bg);
        tv_lcd_tip = (TextView) findViewById(R.id.tv_lcd_tip);
        mTips = findViewById(R.id.tips);
        sb_lcd_brightness= (SeekBar) findViewById(R.id.sb_lcd_brightness);
        sb_lcd_brightness.setMax(MAX_BRIGHTNESS);
        sb_lcd_brightness.setProgress(getSystemBrightness());
        sb_lcd_brightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                changeAppBrightness(progress);
                if(progress==0){
                    minChanged=true;
                }
                if(progress==MAX_BRIGHTNESS){
                    maxChanged=true;
                }
                if(minChanged&&maxChanged){
                    if(mBuilder==null){
                        mBuilder= Contants.showDailog(LCDActivity.this,
                                "是否保存此次测试结果", handler, Gravity.CENTER);
                    }
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        iv_lcd_bg.setOnClickListener(this);
        iv_lcd_bg.setBackgroundColor(RED);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_lcd);
        init();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_lcd_bg) {
            switch (mCurrState) {
                case RED:
                    iv_lcd_bg.setBackgroundColor(GREEN);
                    mCurrState = GREEN;
                    break;
                case GREEN:
                    iv_lcd_bg.setBackgroundColor(BLUE);
                    mCurrState = BLUE;
                    break;
                case BLUE:
                    iv_lcd_bg.setBackgroundColor(BLACK);
                    tv_lcd_tip.setTextColor(Color.WHITE);
                    mCurrState = BLACK;
                    break;
                case BLACK:
                    iv_lcd_bg.setBackgroundColor(WHITE);
                    tv_lcd_tip.setTextColor(Color.BLACK);
                    iv_lcd_bg.setClickable(false);
                    tv_lcd_tip.setText("请滑动调节屏幕亮度");
                    mCurrState = WHITE;
                    sb_lcd_brightness.setVisibility(View.VISIBLE);
                    mTips.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }

     private int getSystemBrightness() {
         int systemBrightness = 0;
         try {
             systemBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
         } catch (Settings.SettingNotFoundException e) {
             e.printStackTrace();
         }
         return systemBrightness;
     }

    private void changeAppBrightness(int brightness) {
        Window window = this.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        if (brightness == -1) {
            lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
        } else {
            lp.screenBrightness = (brightness <= 0 ? 1 : brightness) / 255f;
        }
        window.setAttributes(lp);
    }
}